﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Order_Management
{
    class Program
    {
        static void Main(string[] args)
        {

            OrderService orderService = new OrderService();

            int choice;
            while (true)
            {
                Console.WriteLine("欢迎来到订单管理系统！");
                Console.WriteLine("请选择您要实现的功能：");
                Console.WriteLine("（1）增加订单");
                Console.WriteLine("（2）删除订单");
                Console.WriteLine("（3）修改订单");
                Console.WriteLine("（4）查询订单");
                Console.WriteLine("（5）排序订单");
                Console.WriteLine("（6）存储订单");
                Console.WriteLine("（7）载入订单");
                choice = Convert.ToInt32(Console.ReadLine());

                switch (choice)
                {
                    case 1:
                        {
                            // id orderdetails(customer(name) amount goods(name price number))
                            int id, goodsPrice, goodsNumber, num;
                            string customerName, goodsName;
                            Console.WriteLine("请输入新增订单的订单号：");
                            id = Convert.ToInt32(Console.ReadLine());
                            Console.WriteLine("请输入新增订单的客户名：");
                            customerName = Console.ReadLine();
                            Customer customer = new Customer(customerName);
                            OrderDetails oD = new OrderDetails(customer);
                            Console.WriteLine("请输入订单的商品种类数：");
                            num = Convert.ToInt32(Console.ReadLine());
                            for (int i = 0; i < num; i++)
                            {
                                Console.WriteLine("第" + (i + 1) + "种商品的名字是：");
                                goodsName = Console.ReadLine();
                                Console.WriteLine("第" + (i + 1) + "种商品的数量是：");
                                goodsNumber = Convert.ToInt32(Console.ReadLine());
                                Console.WriteLine("第" + (i + 1) + "种商品的价格是：");
                                goodsPrice = Convert.ToInt32(Console.ReadLine());
                                Goods goods = new Goods(goodsName, goodsNumber, goodsPrice);
                                oD.AddGoods(goods);
                            }
                            Order order = new Order(id, oD);
                            orderService.AddOrder(order);
                            break;
                        }
                    case 2:
                        {
                            int id;
                            Console.WriteLine("请输入您想删除订单的订单号：");
                            id = Convert.ToInt32(Console.ReadLine());
                            orderService.DeleteOrder(id);
                            break;
                        }
                    case 3:
                        {
                            int id;
                            Console.WriteLine("请输入您想修改的订单的订单号：");
                            id = Convert.ToInt32(Console.ReadLine());
                            orderService.ModifyOrder(id);
                            break;
                        }
                    case 4:
                        {
                            int flag;
                            Console.WriteLine("请选择查询的方式：");
                            Console.WriteLine("（1）订单号");
                            Console.WriteLine("（2）商品名称");
                            Console.WriteLine("（3）客户");
                            Console.WriteLine("（4）订单金额");
                            flag = Convert.ToInt32(Console.ReadLine());
                            switch (flag)
                            {
                                case 1:
                                    {
                                        Console.WriteLine("请输入订单号：");
                                        int id = Convert.ToInt32(Console.ReadLine());
                                        var m = from i in orderService.orderList
                                                where (i.id == id)
                                                orderby i.orderDetails.amount
                                                select i;
                                        foreach (var n in m)
                                        {
                                            Console.WriteLine(n.ToString());
                                        }
                                        break;
                                    }
                                case 2:
                                    {
                                        Console.WriteLine("请输入商品名称：");
                                        string name = Console.ReadLine();
                                        int num = orderService.GetNum();
                                        bool[] isMatch = new bool[num];
                                        for (int i = 0; i < num; i++)
                                        {
                                            isMatch[i] = false;
                                        }
                                        foreach (Order n in orderService.orderList)
                                        {
                                            foreach (Goods i in n.orderDetails.goodsList)
                                            {
                                                if (i.name == name)
                                                {
                                                    int index = orderService.orderList.IndexOf(n);
                                                    isMatch[index] = true;
                                                }
                                            }
                                        }
                                        var m = from n in orderService.orderList
                                                where isMatch[orderService.orderList.IndexOf(n)]
                                                select n;
                                        foreach (var i in m)
                                        {
                                            Console.WriteLine(i.ToString());
                                        }
                                        break;
                                    }
                                case 3:
                                    {
                                        Console.WriteLine("请输入客户名：");
                                        string name = Console.ReadLine();
                                        var m = from n in orderService.orderList
                                                where n.orderDetails.GetCustomer() == name
                                                orderby n.orderDetails.amount
                                                select n;
                                        foreach (var i in m)
                                        {
                                            Console.WriteLine(i.ToString());
                                        }
                                        break;
                                    }
                                case 4:
                                    {
                                        Console.WriteLine("请输入订单金额：");
                                        int amount = Convert.ToInt32(Console.ReadLine());
                                        var m = from n in orderService.orderList
                                                where n.orderDetails.amount == amount
                                                orderby n.orderDetails.amount
                                                select n;
                                        foreach (var i in m)
                                        {
                                            Console.WriteLine(i.ToString());
                                        }
                                        break;
                                    }
                                default:
                                    Console.WriteLine("输入错误，请重新输入");
                                    break;
                            }
                            break;
                        }
                    case 5:
                        {
                            int sortChoice;
                            Console.WriteLine("请选择排序方式：");
                            Console.WriteLine("(1)默认排序(按订单号排序) (2)自定义排序");
                            sortChoice = Convert.ToInt32(Console.ReadLine());
                            switch (sortChoice)
                            {
                                case 1:
                                    {
                                        var m = from n in orderService.orderList
                                                orderby n.id
                                                select n;
                                        int num = 0;
                                        foreach (var i in m)
                                        {
                                            orderService.orderList[num] = i;
                                            num++;
                                        }
                                        Console.WriteLine("排序完成");
                                        break;
                                    }
                                case 2:
                                    {
                                        orderService.orderList.Sort();
                                        break;
                                    }
                                default:
                                    {
                                        Console.WriteLine("输入错误，请重新输入");
                                        break;
                                    }
                            }
                            break;
                        }
                    case 6:
                        {
                            orderService.Export();
                            break;
                        }
                    case 7:
                        {
                            orderService.Import();
                            break;
                        }
                    default:
                        {
                            Console.WriteLine("输入错误，请重新输入！");
                            break;
                        }
                }
            }
        }
    }
}